package com.ruijiang.service.UserService.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruijiang.dto.*;
import com.ruijiang.dto.insert.UserInsertDto;
import com.ruijiang.dto.page.UserPageDto;
import com.ruijiang.entity.User;
import com.ruijiang.mapper.UserServiceMapper.UserMapper;
import com.ruijiang.result.PageResult;
import com.ruijiang.service.UserService.UserService;
import com.ruijiang.vo.LoginUserVo;
import com.ruijiang.vo.user.UserGetByIdVO;
import com.ruijiang.vo.user.UserSearchVO;
import com.ruijiang.vo.user.UserTaskCountVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {


    /**
     * 用户登录
     */
    @Autowired
    private UserMapper userMapper;
    @Override
    public LoginUserVo login(LoginDto dto) {

        if(dto == null){
            //参数异常
        }

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getLoginName,dto.getLoginName());
        User user = userMapper.selectOne(wrapper);

        if(user == null){ //代表用户不存在得

        }
        if(!user.getPassword().equals(dto.getPassword())){ //代表密码错误

        }
        //密码相等
        LoginUserVo loginUserVo = new LoginUserVo();
        loginUserVo.setUserId(user.getId());
        loginUserVo.setUserName(user.getUserName());
        loginUserVo.setRoleCode(user.getRoleCode());
        loginUserVo.setSuccess(true);
        loginUserVo.setMsg("登录成功");
        loginUserVo.setToken(UUID.randomUUID().toString());
        return loginUserVo;
    }

    @Override
    public PageResult getList(UserPageDto userPageDto) {

        //执行查询
        PageHelper.startPage(userPageDto.getPageIndex(), userPageDto.getPageSize());
        Page page =(Page) userMapper.getList(userPageDto);
        return PageResult.pageBuild(page);
    }




    /**
     * 新增人员
     * @param userInsertDto
     */
    @Override
    public void insertUser(UserInsertDto userInsertDto) {

        userMapper.insertUser(userInsertDto);

    }

    /**
     * 删除人员
     * @param id
     */
    @Override
    public void deleteUser(Integer id) {
        userMapper.deleteUser(id);
    }

    /**
     * 修改人员
     * @param id
     * @param userInsertDto
     */
    @Override
    public void updateUser(Integer id, UserInsertDto userInsertDto) {
        userMapper.updateUser(id,userInsertDto);

    }


    /**
     * 查询人员
     * @param id
     * @return
     */
    @Override
    public UserGetByIdVO getById(Integer id) {

        UserGetByIdVO userGetByIdVO = userMapper.getById(id);
        return userGetByIdVO;
    }

    /**
     * 根据售货机获取运营人员列表
     * @param innerCode
     * @return
     */
    @Override
    public List<User> getOperatorList(String innerCode) {
        return userMapper.getOperatorList(innerCode);
    }

    /**
     * 分页查询用户工作
     * @param dto
     * @return
     */
    @Override
    public PageResult getUserWorkPage(UserPageDto dto) {
        PageHelper.startPage(dto.getPageIndex(), dto.getPageSize());
        Page page =(Page) userMapper.getUserWorkPage(dto);
        return PageResult.pageBuild(page);
    }

    @Override
    public List<User> getRepairerList(String innerCode) {
        return userMapper.getRepairerList(innerCode);
    }


}

